package com.youku.adSDK.view.components
{
   import flash.display.Sprite;
   import flash.display.Loader;
   import flash.display.SimpleButton;
   import flash.utils.Timer;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.youku.utils.AssetUtil;
   import flash.events.MouseEvent;
   import flash.events.TimerEvent;
   import flash.net.URLRequest;
   import com.youku.adSDK.view.events.PaidLoaderEvent;
   
   public class PaidLoader extends Sprite
   {
      
      public function PaidLoader() {
         super();
         this.init();
      }
      
      private var _loader:Loader;
      
      private var _closeButton:SimpleButton;
      
      private var _timer:Timer;
      
      private function init() : void {
         this._loader = new Loader();
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoaderComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoaderError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoaderError);
         this._closeButton = AssetUtil.getAsset("AD_closer") as SimpleButton;
         this._closeButton.addEventListener(MouseEvent.CLICK,this.onCloseButtonClick);
         this._timer = new Timer(3000,1);
         this._timer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onTimeout);
      }
      
      public function load(param1:String) : void {
         this._loader.load(new URLRequest(param1));
         this._timer.start();
      }
      
      private function onLoaderComplete(param1:Event) : void {
         this._timer.stop();
         addChild(this._loader);
         this._closeButton.y = 0;
         this._closeButton.x = this._loader.width - this._closeButton.width;
         addChild(this._closeButton);
         this.dispatchEvent(new PaidLoaderEvent(PaidLoaderEvent.PAID_LOAD_COMPLETE));
      }
      
      private function onLoaderError(param1:Event = null) : void {
         this.dispatchEvent(new PaidLoaderEvent(PaidLoaderEvent.PAID_LOAD_FAIL));
      }
      
      private function onTimeout(param1:TimerEvent) : void {
         this.onLoaderError();
      }
      
      private function onCloseButtonClick(param1:MouseEvent) : void {
         this.dispatchEvent(new PaidLoaderEvent(PaidLoaderEvent.PAID_CLOSE_BUTTON_CLICK));
      }
   }
}
